<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MaxUserRole
 *
 * @author Mina
 */
class MaxUserRole
{

    /**
     * array of role tasks for each site component
     * @var array
     */
    private $roleTasks;
    private $defaultSiteComponent;
    private $checkCategoryFlag = false;
    private $categories        = array();
    private $roleType;

    /**
     * @param type $roleId 
     */
    public function __construct(User $user)
    {
        $this->checkCategoryFlag = false;
        $this->roleTasks = array(
            SiteComponent::Category => array(),
            SiteComponent::Job => array(),
            SiteComponent::Company => array(),
            SiteComponent::User => array(),
        );
        $role = RoleTable::getInstance()->findRoleWithTasks($user->getRoleId())->execute()->getFirst();
//        $role = $roles[0];
        //if the role is CONTROLLER role check role if need specific 
        $this->roleType = $role->getType();
        if ($role->getType() == Role::CONTROLLER)
        {
            if ($role->getNeedCategory())
            {
                $this->checkCategoryFlag = true;
                $roleCategories = $user->getRoleCategories();
                foreach ($roleCategories as $roleCategory)
                {
                    $this->categories[] = $roleCategory->getCategoryId();
                }
            }
        }
        $this->arrageRoleTasks($role);
    }

    /**
     * arrange tasks  and add each site component with task in one array
     * @param Role $role 
     * @return void
     */
    public function arrageRoleTasks(Role $role)
    {
        $roleTasks = new RoleTasks;

        foreach ($role->getRoleTasks() as $roleTasks)
        {
            $this->roleTasks[$roleTasks->getSiteComponent()->getCode()] = array(
                'viewTask'         => $roleTasks->getViewTask(),
                'addTask'          => $roleTasks->getAddTask(),
                'editTask'         => $roleTasks->getEditTask(),
                'deleteTask'       => $roleTasks->getDeleteTask(),
                'checkTask'        => $roleTasks->getCheckTask(),
                'changeStatusTask' => $roleTasks->getChangeStatusTask()
            );
        }
    }

    /**
     * @param integer $siteComponent from SiteComponent constant
     */
    public function setDefaultSiteComponent($siteComponent)
    {
        $this->defaultSiteComponent = $siteComponent;
    }

    public function checkSiteComponent($siteComponent)
    {
        $return = '';
        if ($siteComponent)
        {
            $return = $siteComponent;
        }
        else
        {
            if ($this->defaultSiteComponent)
            {
                $return = $this->defaultSiteComponent;
            }
            else
            {
                throw new Exception('defaultSiteComponent not define');
            }
        }
        return $return;
    }

    /**
     *
     * @param integer $siteComponent from SiteComponent constant
     * @return boolean 
     */
    public function canView($siteComponent = false)
    {
        $siteComponent = $this->checkSiteComponent($siteComponent);
        return $this->roleTasks[$siteComponent]['viewTask'];
    }

    /**
     *
     * @param integer $siteComponent from SiteComponent constant
     * @return boolean 
     */
    public function canAdd($siteComponent = false)
    {
        $siteComponent = $this->checkSiteComponent($siteComponent);
        return $this->roleTasks[$siteComponent]['addTask'];
    }

    /**
     *
     * @param integer $siteComponent from SiteComponent constant
     * @return boolean 
     */
    public function canEdit($siteComponent = false)
    {
        $siteComponent = $this->checkSiteComponent($siteComponent);
        return $this->roleTasks[$siteComponent]['editTask'];
    }

    /**
     *
     * @param integer $siteComponent from SiteComponent constant
     * @return boolean 
     */
    public function canDelete($siteComponent = false)
    {
        $siteComponent = $this->checkSiteComponent($siteComponent);
        return $this->roleTasks[$siteComponent]['deleteTask'];
    }

    /**
     *
     * @param integer $siteComponent from SiteComponent constant
     * @return boolean 
     */
    public function canCheck($siteComponent = false)
    {
        $siteComponent = $this->checkSiteComponent($siteComponent);
        return $this->roleTasks[$siteComponent]['checkTask'];
    }

    /**
     *
     * @param integer $siteComponent from SiteComponent constant
     * @return boolean 
     */
    public function canChangeStatus($siteComponent = false)
    {
        $siteComponent = $this->checkSiteComponent($siteComponent);
        return $this->roleTasks[$siteComponent]['changeStatusTask'];
    }

    public function checkCategory($categoryId)
    {
        $return = true;
        if ($this->checkCategoryFlag == true)
        {
            if (!in_array($categoryId, $this->categories))
            {
                $return = false;
            }
        }
        return $return;
    }

    /**
     * @return boolean
     */
    public function isAdmin()
    {
        return $this->roleType == Role::CONTROLLER;
    }

    /**
     * @return boolean
     */
    public function isUser()
    {
        return $this->roleType == Role::SITE;
    }

}
